﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Shared
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.Albums;
    public partial class AlbumImage : System.Web.UI.UserControl
    {
        private Guid albumId;
        public Guid AlbumId { get { return albumId; } set { albumId = value; } }
        private string viewImageUrl = string.Empty;
        public string ViewImageUrl { get { return viewImageUrl; } set { viewImageUrl = value; } }
        private string imageIdQueryStringField;
        public string ImageIdQueryStringField { get { return imageIdQueryStringField; } set { imageIdQueryStringField = value; } }
        private string albumIdQueryStringField;
        public string AlbumIdQueryStringField { get { return albumIdQueryStringField; } set { albumIdQueryStringField = value; } }

        protected void Page_Init(object sender, EventArgs e)
        {
            ListView1.ItemDataBound += new EventHandler<ListViewItemEventArgs>(ListView1_ItemDataBound);
            ListView1.ItemCommand += new EventHandler<ListViewCommandEventArgs>(ListView1_ItemCommand);
        }

        void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "ViewImage":
                    ViewImage(ref e);
                    break;
                default: break;
            }
        }

        void ViewImage(ref ListViewCommandEventArgs e)
        {
            Session["SELECTEDIMAGEINDEX"] = Convert.ToInt32(e.CommandArgument);
        }

        void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            ListViewDataItem item = (ListViewDataItem)e.Item;
            Sc_AlbumImage img = (Sc_AlbumImage)((ListViewDataItem)e.Item).DataItem;
            HyperLink lnkViewImage = (HyperLink)e.Item.FindControl("lnkViewImage");
            lnkViewImage.NavigateUrl = string.Format("{0}{1}&pg={2}", viewImageUrl, Request.Url.Query, item.DataItemIndex);
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["ALBUMIMAGELIST"] = AlbumOperations.GetAlbumImages(albumId);
            }

            ListView1.DataSource = Session["ALBUMIMAGELIST"];
            ListView1.DataBind();
        }
    }
}